package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareMagessFamsBinding;
import com.sgb.knowledgeshare.entity.FollowListEntity;
import com.sgb.knowledgeshare.view.adapter.MagessFansAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareMagessFansModel;
import com.sgb.knowledgeshare.viewmodel.ShareMagessLandModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.List;

/**
 * Created by fq on 2020/9/24 0024
 * Description：分享-消息-粉丝
 */
public class ShareMagessFansActivity extends AppActivity {

    ShareMagessFansModel viewModel;
    private ActivityShareMagessFamsBinding binding;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private MagessFansAdapter mDataAdapter;
    private int page;
    private boolean isLoadMore;
    private List<FollowListEntity.RecordsBean> fanslist;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ShareMagessFansActivity.class);
        context.startActivity(intent);
    }



    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_magess_fams);
        View decor = getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.include.titleView.setText("粉丝");
        viewModel = new ViewModelProvider(this).get(ShareMagessFansModel.class);
        viewModel.setActivity(ShareMagessFansActivity.this);
        binding.include.ivBack.setOnClickListener(this);
        mDataAdapter = new MagessFansAdapter(this, null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(getApplicationContext())
                .setHeight(R.dimen.dp5)
                .setPadding(R.dimen.dp0)
                .setColorResource(R.color.white)
                .build();
        binding.lrecyclerview.addItemDecoration(divider);
        binding.lrecyclerview.setLayoutManager(new LinearLayoutManager(this));
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                viewModel.getFensList(page, SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
            }
        });
        mDataAdapter.setFensData(new MagessFansAdapter.FensData() {
            @Override
            public void fens(String UserNo) {
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "").equals(UserNo)) {
                    viewModel.getFens(UserNo);
                } else {
                    MToast.showToast(getApplicationContext(), "自己不能关注自己");
                }
            }
        });
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //点击用户跳转到当前用户主页,加载用户主页信息
                ShareUserActivity.start(getApplicationContext(), fanslist.get(position).getUserNo());
            }
        });
    }

    @Override
    protected void init() {
        super.init();
        initData();
    }

    private void initData() {
        isLoadMore = false;
        page = 1;
        viewModel.getFensList(page, SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    public void setFensList(FollowListEntity data) {
        if (data == null) {
            return;
        }
        this.fanslist = data.getRecords();
        binding.lrecyclerview.refreshComplete(page);
        binding.lrecyclerview.setPullRefreshEnabled(true);
        if (isLoadMore) {
            if (data.getRecords().size() > 0) {
                mDataAdapter.addDatas(data.getRecords());
            } else {
                MToast.showToast(getApplicationContext(), "没有数据啦");
            }
        } else {
            if (data.getRecords().size() > 0) {
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                mDataAdapter.setDatas(data.getRecords());
            } else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
                mDataAdapter.setDatas(null);
            }
        }
    }

    public void setFens() {
        initData();
    }
}
